import BaseSprite from "../../Base/BaseSprite";
import { EBundleType } from "../../Common/Enum";
import { TalkModule } from "../../Common/TalkModule";
import { IBaseSkeData, ITalkData } from "../../Interface/IMessage";
import { SceneGame } from "../../SceneGame";
import AudioManager from "../../Util/AudioManager";
import GameUtil from "../../Util/GameUtil";
import { LoadUtil } from "../../Util/LoadUtil";
import StorageUtil from "../../Util/StorageUtil";
import CartoonUI from "../home/CartoonUI";

const { ccclass, property } = cc._decorator;

@ccclass
export default class HospitalTalk extends BaseSprite {
    public static instance: HospitalTalk = null;

    @property({ type: sp.Skeleton, tooltip: "神医" })
    private ske: sp.Skeleton = null;

    @property({ type: cc.Node, tooltip: "冰封按钮" })
    private bingNode: cc.Node = null;
    @property({ type: cc.Node, tooltip: "冰封按钮" })
    private btnBing: cc.Node = null;
    @property({ type: sp.Skeleton, tooltip: "冰动画" })
    private skeBing: sp.Skeleton = null;
    @property({ type: [cc.Node], tooltip: "冰封图" })
    private bing: cc.Node[] = [];


    private talkData: ITalkData = null;

    protected onLoad() {
        super.onLoad();
        HospitalTalk.instance = this;

        this.talkData = StorageUtil.instance.getTalkData();

        this.isActive(true);
    }

    public async isActive(isActive: boolean = true) {
        // TODO 弹窗出现后刷新ui层展示，所以做延时
        await GameUtil.sleep(350);
        this.talkData = StorageUtil.instance.getTalkData();
        this.node.active = isActive;
        if (isActive) {
            SceneGame.instance.hideTopUI();
        } else {
            SceneGame.instance.showTopUI();
        }

        this.bingActive();
    }

    /** 展示漫画 */
    public showCartoon() {
        // 通过弹窗的方式添加会展示UI层，所以单独处理
        LoadUtil.instance.loadRes(EBundleType.Game, 'prefab/panel/CartoonUI', cc.Prefab, (err, res) => {
            const node = cc.instantiate(res) as cc.Node;
            node.parent = SceneGame.instance.getToastNode();
            node.getComponent(CartoonUI).init(2, () => {
                this.talkData.id = 10609;
                this.playAnimation();
            });
            console.log("漫画this.talkData.id:", this.talkData.id);
        });
    }

    public playAnimation() {
        switch (this.talkData.id) {
            case 10609:
                {
                    const data: IBaseSkeData = {
                        ske: this.ske,
                        animName: "jing",
                        isLoop: false,
                        hideBones: ['头正常张嘴'],
                        callback: () => {
                            this.ske.setAnimation(0, 'idle', true);
                            this.talkData.id = 10706;
                            TalkModule.updateCheck();
                        }
                    };
                    GameUtil.playAnimation(data);
                    console.log("播放动画:", data.animName);
                }
                break;
            case 10710:
                {
                    const data: IBaseSkeData = {
                        ske: this.ske,
                        animName: "buxie",
                        isLoop: true,
                        loopCount: 2,
                        hideBones: ['震惊符号'],
                        callback: () => {
                            this.ske.setAnimation(0, 'idle', true);
                        }
                    };
                    GameUtil.playAnimation(data);
                    console.log("播放动画:", data.animName);
                }
                break;
            case 10711:
                {
                    this.bingActive();
                }
                break;
        }
    }

    public showMouth() {
        const bone = this.ske.findBone('头正常张嘴');
        if (bone) {
            bone.active = true;
        }
    }

    public hideMouth() {
        const bone = this.ske.findBone('头正常张嘴');
        if (bone) {
            bone.active = false;
        }
    }

    public playPaiZhuoZi(callback: Function) {
        AudioManager.instance.playSound('拍桌子');
        const data: IBaseSkeData = {
            ske: this.ske,
            animName: "paizhuozi",
            isLoop: false,
            callback: () => {
                // const data1: IBaseSkeData = {
                //     ske: this.ske,
                //     animName: "shegnqi",
                //     isLoop: true,
                // };
                // GameUtil.playAnimation(data1);
                callback && callback();
                callback = null;
            }
        };
        GameUtil.playAnimation(data);
    }

    private bingIndex: number = 0;
    private bingFeng() {
        this.bingIndex++;
        this.bing.forEach((o, i) => {
            o.active = i == this.bingIndex;
        });
        if (this.bingIndex > this.bing.length) {
            // 冰封结束
            const talkData = StorageUtil.instance.getTalkData();
            talkData.id = 10800;
            this.bingActive();
            TalkModule.updateCheck();
        }
    }

    public bingActive() {
        this.bingNode.active = this.talkData.id === 10711;
        this.btnBing.active = this.talkData.id === 10711;
        if (this.talkData.id === 10711) {
            this.hideMouth();
            SceneGame.instance.showGuideUI(this.btnBing, 1.7);
        }
    }

    private count: number = 0;
    private onBtnsClicked(event: cc.Event.EventTouch, eventData: string) {
        const name = event.target.name;
        switch (name) {
            case 'btnBing':
                {
                    SceneGame.instance.hideGuideUI();
                    // 最后一次点一次
                    if (this.bingIndex === 4) {
                        this.bingFeng();
                        return;
                    }
                    this.count++;
                    if (this.count >= 4) {
                        this.count = 0;
                        this.skeBing.node.active = true;
                        const data: IBaseSkeData = {
                            ske: this.skeBing,
                            animName: "anniu",
                            isLoop: false,
                            callback: () => {
                                this.bingFeng();
                            }
                        }
                        GameUtil.playAnimation(data);
                        AudioManager.instance.playSound("冰块破碎");
                    } else {
                        AudioManager.instance.playSound("点击冰裂开");
                    }
                }
                break;
        }
    }
}